﻿let ev = require("./getConfig").ev;
let config1 = require(ev.path);
let cluster = require('cluster');
let minport = ev.minport;
let maxport = ev.maxport;
let portList = ev.portList;
let offsetport = ev.offsetport || 0;
let threadListennum = config1.threndListenNum || 50;
if ((minport == undefined || maxport == undefined) && portList == undefined) {
    console.log("定义命令行参数{minport，maxport}||portList为Array");
    process.exit(0);
}
let tcpen = ev.tcpen ? true : false;
let udpen = ev.udpen ? true : false;
if (!(tcpen || udpen)) {
    console.log("必须开启其中之一tcpen|udpen| ");
    process.exit(0);
}


if (!config1.list)
    throw console.log("配置错误");
let serverList = config1.list;

if (cluster.isMaster) {//主线程
    // mainProcess();
    portList = getPortList(); //环境
    if (portList == undefined) process.exit(0);
    mainRunTcps();
} else {
    //initchild();
    console.log("child pid start")
}

function getPortList() {
    let res;
    if (portList && portList.constructor == Array) {
        res = portList;
        return res;
    }
    res = new Array(maxport - minport + 1);
    for (let i = minport, j = 0; i <= maxport; i++ , j++) {
        res[j] = i;
    }

    return res;
}


function monitor() {
    setInterval(function report() {
        process.send({ memory: process.memoryUsage(), process: process.pid });
    }, 10000);
}
function forkWorker(listener) {//参数回调
    let worker = cluster.fork();
    console.log('worker [%d] has been created', worker.process.pid);
    worker.on('message', listener);
    return worker;
}

function mainProcess() {// 不起作用
    console.log(JSON.stringify(serverList));
    for (let index = 0, len = config1.list.length * 2; index < len; index++) {
        /*        let s = forkWorker(function(){
         console.log("主线程回掉")
         });*/
        exec("node ./childprocess " + JSON.stringify(JSON.stringify({ index })))
        //console.log("子进程启动：" + s.process.pid);
        /*        s.send({
         type: 'createServer',
         value: index
         });*/
    }

}
//Cluster收到子进程退出消息
cluster.on('exit', function (worker, code, signal) {
    console.log('worker [%d] died %s, fork a new one.',
        worker.process.pid, code || signal);
    //forkWorker(workerListener);
});
//Cluster收到子进程运行消息
cluster.on('online', function (worker) {
    console.log('worker [%d] is running.', worker.process.pid);
});
process.on('uncaughtException', function () {

});
function mainRunTcps() {
    let threadnum = Math.ceil(portList.length / threadListennum);
    console.log(`你即将启动${threadnum}个进程, 监听${portList.length}个端口`);
    for (let i = 0; i < threadnum; i++) {
        /*        let st = i * threadListennum + minport;
                let en = (i + 1) * threadListennum + minport - 1;*/
        //  en = (en > maxport) ? maxport:en;
        let tmp = portList.splice(0, threadListennum);
        console.log(tmp);
        let temev = { tcpen: tcpen, udpen: udpen, portList: tmp, offsetport: offsetport }
        exec("node ./tcpList " + JSON.stringify(JSON.stringify(temev)))
    }
}
